package com.atguigu.gmall.all.controller;

import com.atguigu.gmall.activity.client.ActivityFeignClient;
import com.atguigu.gmall.common.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author atguigu-mqx
 */
@Controller
public class SeckillController {

    @Autowired
    private ActivityFeignClient activityFeignClient;

    //  http://activity.gmall.com/seckill.html
    @GetMapping("seckill.html")
    public String seckillList(Model model){
        //  后端需要存储 list  ${list}
        Result result = activityFeignClient.findAll();
        //  保存数据！
        model.addAttribute("list",result.getData());
        return "seckill/index";
    }

    //  th:href="'/seckill/'+${item.skuId}+'.html'"
    @GetMapping("seckill/{skuId}.html")
    public String seckillSkuId(@PathVariable Long skuId,Model model){
        //  ${item} 指的的是SeckillGoods
        Result result = activityFeignClient.getSeckillGoods(skuId);
        //  存储数据
        model.addAttribute("item",result.getData());
        //  返回数据
        return "seckill/item";
    }

    //  http://activity.gmall.com/seckill/queue.html?skuId=22&skuIdStr=c81e728d9d4c2f636f067f89cc14862c
    @GetMapping("seckill/queue.html")
    public String queue(HttpServletRequest request){
        //  需要保存：
        String skuId = request.getParameter("skuId");
        String skuIdStr = request.getParameter("skuIdStr");
        request.setAttribute("skuId",skuId);
        request.setAttribute("skuIdStr",skuIdStr);
        //  返回排队页面！
        return "seckill/queue";
    }

    //  去下单控制器：
    @GetMapping("seckill/trade.html")
    public String seckillTrade(Model model){
        //  后台需要存储 userAddressList detailArrayList totalAmount totalNum
        Result<Map<String,Object>> result =  this.activityFeignClient.trade();
        //  判断
        if (result.isOk()){
            model.addAllAttributes(result.getData());
            return "seckill/trade";
        }else {
            //  ${message}
            model.addAttribute("message","下单失败!");
            return "seckill/fail";
        }


    }
}
